<?php
/**
 * Created by PhpStorm.
 * User: nightelf
 * Date: 2020/1/11
 * Time: 11:29
 */

namespace App\Http\Model;

use Illuminate\Support\Facades\DB;

class WorkmanAccountsModel1 extends Model
{
    const TABLE = 'workman_accounts';

    /**
     * @param array $data
     * @return mixed
     */
    public function lists(array $data)
    {
        return DB::table(self::TABLE)
            ->leftJoin('employee as e','e.id','=',self::TABLE.'.employeeId')
            ->leftJoin('admin as a','a.id','=',self::TABLE.'.adminId')
            ->where(self::TABLE.'.projectId',$data['projectId'])
            ->where('sectionId',$data['sectionId'])
            ->where(self::TABLE.'.groupId',$data['groupId'])
            ->where(function ($query) use ($data){
                if (isset($data['batch']) && $data['batch'] !== ''){
                    $query->where(self::TABLE.'.batch',$data['batch']);
                }
            })
            ->groupBy(self::TABLE.'.employeeId')
            ->select(self::TABLE.'.*','e.name as employeeName','e.jobNumber','a.name as adminName',DB::raw('SUM(account) as account'))
            ->get()->toArray();
    }

    /**
     * 修改中
     * @param array $param
     * @return array
     * @throws \Exception
     */
    public function lists2(array $param)
    {
        $yearInfo = (new YearModel())->findYear(date('Y-m-d H:i:s'));
        $employeeLists = DB::table(self::TABLE)
            ->leftJoin('employee as e', 'e.id', '=', self::TABLE . '.employeeId')
            ->leftJoin('project', 'project.id', '=', self::TABLE . '.projectId')
            ->leftJoin('profession as p', 'p.id', '=', 'e.professionId')
            ->where(function ($query) use ($param){
                if (!empty($param['projectId'])){
                    $query->where(self::TABLE.'.projectId',$param['projectId']);
                }
            })
            ->where('e.entryTime','>=',$yearInfo['startTime'])
            ->where('e.entryTime','<=',$yearInfo['endTime'])
            ->orderBy('e.status','asc')
            ->groupBy('employeeId')
            ->select('employeeId',self::TABLE.'.status','e.name as employeeName', 'e.jobNumber','p.name as professionName','project.name as projectName','e.status as employeeStatus')
            ->get()->toArray();

        //获取月杂工情况，不传月份则默认为当前月份
        $startTime = (new \DateTime($param['month']))->format('Y-m-01');
        $endTime = (new \DateTime($param['month']))->format('Y-m-t');
        foreach ($employeeLists as $key=> $e) {
            $dayList = $this->datePeriod($startTime, $endTime, '1 day', 'j');
            $separate = DB::table(self::TABLE)
                ->where('employeeId',$e->employeeId)
                ->where(function ($query) use ($param){
                    if (!empty($param['projectId'])){
                        $query->where(self::TABLE.'.projectId',$param['projectId']);
                    }
                })
                ->whereBetween('separateTime',[$startTime,$endTime])
                ->groupBy('separateTime')
                ->select('separateTime',DB::raw('SUM(account) as accounts'))
                ->get()->toArray();
            if(empty($separate)){
                unset($employeeLists[$key]);
            }else{
                foreach ($separate as $a) {
                    $d = (new \DateTime($a->separateTime))->format('j');
                    if (is_array($dayList[$d])){
                        $dayList[$d] = $a->accounts;
                    }else{
                        $dayList[$d] += $a->accounts;
                    }
                }
                $employeeLists[$key]->accounts = $dayList;
            }
        }
        return array_values($employeeLists);
    }
}